var dayjs = require('dayjs');

Component({
    options:{
        styleIsolation:'isolated'
    },
    properties:{
        countDate:{
            type:String,
            value:"0",
            observer: function (value) {
                if(!value){
                    this.init_date('DAY');
                }
                else{
                    this.setData({
                        start:value.split(",")[0],
                        end:value.split(",")[1]
                    });
                }
            }
        },
        dataType:{
            type:String,
            value:""
        },
    },
    data:{
        dataIndex:'',
        isOpen:false,
        dateItemList:[{
            label:"日度",
            value:"DAY"
        },{
            label:"月度",
            value:"MONTH"
        }],
        start:"",
        end:"",
        dataType:""
    },
    lifetimes: {
        // attached(){
        //     this.init_date(this.properties.dataType);
        // }
    },
    methods: {
        init_date(dataType){
            let start, end;
            if( dataType == 'MONTH'){
                start = dayjs().startOf("month").format("YYYY-MM-DD");
                end = dayjs().format("YYYY-MM-DD")
            }
            else if(dataType == 'DAY'){
                start = dayjs().format("YYYY-MM-DD");
                end = dayjs().format("YYYY-MM-DD")
            }
            this.setData({
                start:start,
                end:end,
                dataType:dataType
            });
            this.triggerEvent("change",{
                dataType:dataType,
                countDate:start+','+end
            });
        },
        confirm_date_item(e){
            this.init_date(e.detail.value);
        },
        top_picker_confirm(e){
            this.triggerEvent("change",{
                dataType:this.data.dataType,
                countDate:e.detail[0]+','+e.detail[1]
            });
        }
    }
})
